package com.fare.taluo.application.impl.fetl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.fare.taluo.application.fetl.SinglePlaneInfoApplication;
import com.fare.taluo.fetl.domain.entity.PlaneInfoJsonEntity;
import com.fare.taluo.fetl.domain.entity.SinglePlaneInfoEntity;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.util.PlaneInfoVOToPlaneInfo;
import com.fare.taluo.util.StringUtils;
import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.vo.single.AgentVO;
import com.fare.taluo.vo.single.CabinVO;
import com.fare.taluo.vo.single.PlaneInfoJsonVO;
import com.fare.taluo.vo.single.SinglePlaneInfoVO;
import com.fare.taluo.vo.single.StopOverVO;
import com.fare.taluo.vo.single.TransitVO;

@Scope("singleton")
@Service("singlePlaneInfoApplication")
public class SinglePlaneInfoApplicationImpl implements
		SinglePlaneInfoApplication {

	@Override
	public List<SinglePlaneInfoVO> findPlaneInfoProperty(String hbaseKey,
			String fromCity, String toCity, String flightBatchDate,
			String flightNo) {
		List<SinglePlaneInfoVO> listVo=new ArrayList<SinglePlaneInfoVO>();
		SinglePlaneInfoEntity entity =new SinglePlaneInfoEntity();
		List<SinglePlaneInfoEntity> listEntity =entity.findPlaneInfoProperty(hbaseKey, fromCity, toCity, flightBatchDate, flightNo);
		if(null!=listEntity && listEntity.size()>0){
			for(SinglePlaneInfoEntity en : listEntity){
				SinglePlaneInfoVO vo = this.planeInfoEntityToVo(en);
				if(null!=vo && null!=vo.getFlightNo()){
					listVo.add(vo);
				}
			}
		}
		return listVo;
	}
	@Override
	public List<SinglePlaneInfoEntity> findPlaneInfo(String grabChannel,String carrierKey,
			String fromCity, String toCity, String flightBatchDate,
			String flightNo){
		return new SinglePlaneInfoEntity()
			.findPlaneInfoByCondition(grabChannel, carrierKey, fromCity, toCity, flightBatchDate, flightNo);
	}
	
	public SinglePlaneInfoVO planeInfoEntityToVo(SinglePlaneInfoEntity entity){
		SinglePlaneInfoVO vo =new SinglePlaneInfoVO();
		vo.setActuallyFlightNo(entity.getActuallyFlightNo());
		vo.setAgentName(entity.getAgentName());
		vo.setAreaCode(entity.getAreaCode());
		vo.setAreaName(entity.getAgentName());
		vo.setAttachHbaseKey(entity.getAttachHbaseKey());
		vo.setCarrierFullName(entity.getCarrierFullName());
		vo.setCarrierKey(entity.getCarrierKey());
		vo.setCarrierName(entity.getCarrierName());
		vo.setCreateTime(entity.getCreateTime());
		vo.setEndTime(entity.getEndTime());
		vo.setFlightDate(entity.getFlightDate());
		vo.setFlightNo(entity.getFlightNo());
		vo.setFlightType(entity.getFlightType());
		vo.setFromCity(entity.getFromCity());
		vo.setFromCityName(entity.getFromCityName());
		vo.setGrabChannelId(entity.getGrabChannelId());
		vo.setGrabChannelName(entity.getGrabChannelName());
		vo.setGrabDate(entity.getGrabDate());
		vo.setId(entity.getId());
		vo.setSourceRowkey(entity.getSourceRowkey());
		vo.setStartTime(entity.getStartTime());
		vo.setToCity(entity.getToCity());
		vo.setToCityName(entity.getToCityName());
		vo.setTotalHighestPrice(entity.getTotalHighestPrice());
		vo.setTotalLowestPrice(entity.getTotalLowestPrice());
		if(null!=entity.getPlaneInfoJsonEntity()){
			vo.setPlaneInfoJsonVO(this.planeInfoJsonEntityToVO(entity.getPlaneInfoJsonEntity()));
			if(null!=vo.getPlaneInfoJsonVO().getAgentsJson()){
			  Set<AgentVO> setAgents =new HashSet<AgentVO>(JsonUtil.getDTOList(vo.getPlaneInfoJsonVO().getAgentsJson(), AgentVO.class));
			  vo.setAgents(setAgents);
			}
			if(null!=vo.getPlaneInfoJsonVO().getCabinsJson()){
				Set<CabinVO> setCabins =new HashSet<CabinVO>(JsonUtil.getDTOList(vo.getPlaneInfoJsonVO().getCabinsJson(), CabinVO.class));
				vo.setCabins(setCabins);
			}
			if(null!=vo.getPlaneInfoJsonVO().getTransitsJson()){
				Set<TransitVO> setTransit =new HashSet<TransitVO>(JsonUtil.getDTOList(vo.getPlaneInfoJsonVO().getTransitsJson(), TransitVO.class));
				vo.setTransits(setTransit);
			}
			if(null!=vo.getPlaneInfoJsonVO().getStopOversJson()){
				Set<StopOverVO> setStopOver =new HashSet<StopOverVO>(JsonUtil.getDTOList(vo.getPlaneInfoJsonVO().getStopOversJson(), StopOverVO.class));
				vo.setStopOvers(setStopOver);
			}
		}
		
		return vo;
	}
	
	public PlaneInfoJsonVO planeInfoJsonEntityToVO(PlaneInfoJsonEntity entity){
		PlaneInfoJsonVO vo =new PlaneInfoJsonVO();
		vo.setId(entity.getId());
		vo.setPlaneInfoId(entity.getId());
		vo.setAgentsJson(entity.getAgentsJson());
		vo.setCabinsJson(entity.getCabinsJson());
		vo.setStopOversJson(entity.getStopOversJson());
		vo.setTransitsJson(entity.getTransitsJson());
		return vo;
	}
	
/*	public static void main(String[] args) {
		SinglePlaneInfoApplication demo = InstanceFactory.getInstance(SinglePlaneInfoApplication.class);
		System.out.println(demo.findPlaneInfoProperty("20005", "XIY", "PEK", "2014-03-05", "CZ5415"));
	}*/

	@Override
	public List<CabinVO> findPlaneInfoByCondition(String grabChannel,String carrierKey,
			String fromCity, String toCity, String flightBatchDate,
			String flightNo) {
		List<SinglePlaneInfoVO> listVo=new ArrayList<SinglePlaneInfoVO>();
		List<CabinVO> cabinVOs=new ArrayList<CabinVO>();
		SinglePlaneInfoEntity entity =new SinglePlaneInfoEntity();
		List<SinglePlaneInfoEntity> listEntity = entity.findPlaneInfoByCondition(grabChannel,carrierKey, fromCity, toCity, flightBatchDate, flightNo);
		
		
		
		if(StringUtils.checkList(listEntity)){
			for(SinglePlaneInfoEntity en : listEntity){
				SinglePlaneInfoVO vo = this.planeInfoEntityToVo(en);
				if(null!=vo && null!=vo.getFlightNo()){
					listVo.add(vo);
				}
			}
			SinglePlaneInfoVO en = listVo.get(0);
			for (CabinVO vo : en.getCabins()) {  
				///vo.setCabinType("test");
				if(StringUtils.isNotEmpty(vo.getCabinType())){
					vo.setCarrierFlightnoCabin(en.getCarrierKey()+"_"+en.getFlightNo()+"_"+vo.getCabinType()+"_"+en.getCarrierName()+"_"+en.getAttachHbaseKey()+"_"+en.getGrabChannelName());
					//vo.setCabinType("test");
					cabinVOs.add(vo);
				}
				/*vo.setCabinType("test");
				cabinVOs.add(vo);*/
				
			}
		}
		//将舱位类型去重
		cabinVOs = distinctCabins(cabinVOs);
		return cabinVOs;
	}
	/**
	 * 舱位类型去重
	 * @param cabinVOs
	 * @return
	 */
	private List<CabinVO>  distinctCabins(List<CabinVO> cabinVOs){
		List<CabinVO> list = new ArrayList<CabinVO>();
		Map<String,CabinVO> map = new HashMap<String,CabinVO>();
		for (CabinVO cabinVO : cabinVOs) {
			map.put(cabinVO.getCabinType(), cabinVO);
		}
		list.addAll(map.values());
		return list;
	}
	@Override
	public List<PlaneInfo> findPlaneInfo(String hbaseKey, String fromCity,
			String toCity, String flightBatchDate, String flightNo) {
		List<PlaneInfo> listPlaneInfo =new ArrayList<PlaneInfo>();
		List<SinglePlaneInfoVO> listVo=new ArrayList<SinglePlaneInfoVO>();
		SinglePlaneInfoEntity entity =new SinglePlaneInfoEntity();
		List<SinglePlaneInfoEntity> listEntity =entity.findPlaneInfoPropertyAll(hbaseKey, fromCity, toCity, flightBatchDate, flightNo);
		if(null!=listEntity && listEntity.size()>0){
			for(SinglePlaneInfoEntity en : listEntity){
				SinglePlaneInfoVO vo = this.planeInfoEntityToVo(en);
				if(null!=vo && null!=vo.getFlightNo()){
					listVo.add(vo);
				}
			}
		}
		if(null!=listVo && listVo.size()>0){
			for(SinglePlaneInfoVO vo :listVo){
				PlaneInfo planeInfo =PlaneInfoVOToPlaneInfo.singleToPlanInfo(vo);
				listPlaneInfo.add(planeInfo);
			}
		}
		return listPlaneInfo;
	}
	@Override
	public List<PlaneInfo> queryFreightPrice(String hbaseKey,
			String carrierKey, String fromCity, String toCity,
			String flightBatchDate) {
		List<PlaneInfo> listPlaneInfo =new ArrayList<PlaneInfo>();
		List<SinglePlaneInfoVO> listVo=new ArrayList<SinglePlaneInfoVO>();
		List<SinglePlaneInfoEntity> listEntity = this.findPlaneInfo(hbaseKey, carrierKey, 
				fromCity, toCity, flightBatchDate, null);
		if(null!=listEntity && listEntity.size()>0){
			for(SinglePlaneInfoEntity en : listEntity){
				SinglePlaneInfoVO vo = this.planeInfoEntityToVo(en);
				if(null!=vo && null!=vo.getFlightNo()){
					listVo.add(vo);
				}
			}
		}
		if(null!=listVo && listVo.size()>0){
			for(SinglePlaneInfoVO vo :listVo){
				PlaneInfo planeInfo =PlaneInfoVOToPlaneInfo.singleToPlanInfo(vo);
				listPlaneInfo.add(planeInfo);
			}
		}
		return listPlaneInfo;
	}
}
